package com.yh.test.web.controller;

import com.google.openrtb.OpenRtb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

/**
 * Created by uu on 2017/2/10.
 */
@Controller
@RequestMapping("/protobuf")
public class ProtoBufController {

    @RequestMapping(value = "/protobuf_adx", method = RequestMethod.GET)
    public void protobuf () throws IOException {
        OutputStream out = null;
        URL url = new URL("http://localhost:8084/protobuf/protobuf_adx2");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-protobuf");
        conn.setRequestProperty("Accept", "application/x-protobuf");
        conn.connect();

        out = conn.getOutputStream();
        OpenRtb.BidRequest bid = OpenRtb.BidRequest.newBuilder()
                .setId("1234534625254")
                .setAt(OpenRtb.BidRequest.AuctionType.SECOND_PRICE)
                .setTmax(120)
                .addImp(OpenRtb.BidRequest.Imp.newBuilder()
                        .setId("1")
                        .setBanner(OpenRtb.BidRequest.Imp.Banner.newBuilder()
                                .setW(300)
                                .setH(250)
                                .setPos(OpenRtb.BidRequest.Imp.AdPosition.ABOVE_THE_FOLD)
                                .addBattr(OpenRtb.CreativeAttribute.USER_INTERACTIVE)))
                .addBadv("company1.com")
                .addBadv("company2.com")
                .setSite(OpenRtb.BidRequest.Site.newBuilder()
                        .setId("234563")
                        .setName("Site ABCD")
                        .setDomain("siteabcd.com")
                        .addAllCat(Arrays.asList("IAB2-1", "IAB2-2"))
                        .setPrivacypolicy(true)
                        .setPage("http://siteabcd.com/page.htm")
                        .setRef("http://referringsite.com/referringpage.htm")
                        .setPublisher(OpenRtb.BidRequest.Publisher.newBuilder()
                                .setId("pub12345")
                                .setName("Publisher A"))
                        .setContent(OpenRtb.BidRequest.Content.newBuilder()
                                .setKeywords("keyword a,keyword b,keyword c")))
                .setDevice(OpenRtb.BidRequest.Device.newBuilder()
                        .setIp("64.124.253.1")
                        .setUa("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.16)")
                        .setOs("OS X")
                        .setFlashver("10.1")
                        .setJs(true))
                .setUser(OpenRtb.BidRequest.User.newBuilder()
                        .setId("45asdf987656789adfad4678rew656789")
                        .setBuyeruid("5df678asd8987656asdf78987654"))
                .build();
        byte[] messageBody = bid.toByteArray();
        out.write(messageBody);
        out.flush();
        out.close();

        int code = conn.getResponseCode();
        boolean success = (code >= 200) && (code < 300);
        InputStream in = success ? conn.getInputStream() : conn.getErrorStream();
        OpenRtb.BidResponse res = OpenRtb.BidResponse.parseFrom(in);
        in.close();
        System.out.println("返回的消息:");
        System.out.println(res);
    }
}
